const Router = require('@koa/router')
const {shopClass} = require('../tool')
const router = new Router()

const db = require('../db')
const {shoppingCart} = require('../vri')

// 从id获取
router.get('/fromid', async (ctx) => {
  const {query} = ctx
  const {adopt, error, value} = shoppingCart.fromid.verifies(query)
  if (!adopt) return ctx.error(430, {error: error.toString()})
  const data = await db.first({
    form: 'shopping-cart',
    virtual: ['commodity', 'shop'],
    where: {
      id: value.id,
    },
  })
  if (!data) return ctx.error(431)
  ctx.body = {
    code: 200,
    data,
  }
})

// 获取
router.get('/list', async (ctx) => {
  const {user} = ctx.state
  if (!user) return ctx.error('401')
  const yd = await db.find({
    form: 'shopping-cart',
    where: {
      settle: 0,
      userid: user.id,
    },
    virtual: ['commodity', 'shop'],
    order: {
      createdAt: 'DESC',
    },
  })
  const data = shopClass(yd)
  ctx.body = {
    code: 200,
    data,
  }
})

// 添加
router.post('/add', async (ctx) => {
  const {user} = ctx.state
  if (!user) return ctx.error('401')
  const {body} = ctx.request
  const {adopt, error, value} = shoppingCart.add.verifies(body)
  if (!adopt) return ctx.error(430, {error: error.toString()})
  const cy = await db.first({
    form: 'commodity',
    virtual: ['cover'],
    where: {
      id: value['commodityid'],
    },
  })
  if (!cy) return ctx.error('431')
  const {affectedRows} = await db.update({
    form: 'shopping-cart',
    where: {
      commodityid: value.commodityid,
      specification: value.specification,
      userid: value.userid,
      shopid: cy.shopid,
    },
    set: {
      number: value.number,
    },
  })
  if (affectedRows)
    return (ctx.body = {
      code: 200,
      msg: '添加成功',
      commodity: cy,
    })
  await db.insert({
    form: 'shopping-cart',
    value: {
      ...value,
      settle: 0,
      userid: user.id,
      shopid: cy.shopid,
    },
  })
  ctx.body = {
    code: 200,
    msg: '添加成功',
    commodity: cy,
  }
})

// 选中/取消选中商品
router.get('/select', async (ctx) => {
  const {user} = ctx.state
  if (!user) return ctx.error('401')
  const {query} = ctx
  const {adopt, error, value} = shoppingCart.select.verifies(query)
  if (!adopt) return ctx.error(430, {error: error.toString()})
  const {affectedRows} = await db.update({
    form: 'shopping-cart',
    where: {
      id: value.id,
      userid: user.id,
      settle: 0
    },
    set: {
      selected: value.selected,
    },
  })
  if (!affectedRows) return ctx.error(431)
  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

// 选择/取消选中店铺
router.get('/select/shop', async (ctx) => {
  const {user} = ctx.state
  if (!user) return ctx.error('401')
  const {query} = ctx
  const {adopt, error, value} = shoppingCart.select.verifies(query)
  if (!adopt) return ctx.error(430, {error: error.toString()})
  const {affectedRows} = await db.update({
    form: 'shopping-cart',
    where: {
      shopid: value.id,
      userid: user.id,
      settle: 0
    },
    set: {
      selected: value.selected,
    },
  })
  if (!affectedRows) return ctx.error(431)
  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

// 选择/取消选中店铺
router.get('/select/all', async (ctx) => {
  const {user} = ctx.state
  if (!user) return ctx.error('401')
  const {query} = ctx
  const {adopt, error, value} = shoppingCart.selectAll.verifies(query)
  if (!adopt) return ctx.error(430, {error: error.toString()})
  const {affectedRows} = await db.update({
    form: 'shopping-cart',
    where: {
      userid: user.id,
      settle: 0
    },
    set: {
      selected: value.selected,
    },
  })
  if (!affectedRows) return ctx.error(431)
  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})


// 清空购物车
router.post('/empty', async (ctx) => {
  const {user} = ctx.state
  if (!user) return ctx.error('401')
  const {affectedRows} = await db.delete({
    form: 'shopping-cart',
    where: {
      userid: user.id,
    },
  })
  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

// 修改购买数量
router.get('/number', async (ctx) => {
  const {user} = ctx.state
  if (!user) return ctx.error('401')
  const {query} = ctx
  const {adopt, error, value} = shoppingCart.number.verifies(query)
  if (!adopt) return ctx.error(430, {error: error.toString()})
  const commodity = await db.first({
    form: 'shopping-cart',
    field: ['commodityid'],
    where: {
      id: value.id,
      userid: user.id,
    },
  })
  if (!commodity) return ctx.error(431)
  const {stock} = await db.first({
    form: 'commodity',
    field: ['stock'],
    where: {
      id: commodity.id,
    },
  })
  if (value.number > stock)
    return (ctx.body = {
      code: 432,
      msg: '超出库存',
    })
  const {affectedRows} = await db.update({
    form: 'shopping-cart',
    where: {
      id: value.id,
      userid: user.id,
    },
    set: {
      number: value.number,
    },
  })
  if (!affectedRows) return ctx.error(431)
  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

// 删除
router.post('/del', async (ctx) => {
  const {user} = ctx.state
  if (!user) return ctx.error('401')
  const {body} = ctx.request
  const {adopt, error, value} = shoppingCart.del.verifies(body)
  if (!adopt) return ctx.error(430, {error: error.toString()})
  const {affectedRows} = await db.delete({
    form: 'shopping-cart',
    where: {
      id: value.id,
      userid: user.id,
    },
  })
  if (!affectedRows) return ctx.error(431)
  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

// 批量删除
router.post('/dels', async (ctx) => {
  const {user} = ctx.state
  if (!user) return ctx.error('401')
  const {body} = ctx.request
  const {adopt, error, value} = shoppingCart.dels.verifies(body)
  if (!adopt) return ctx.error(430, {error: error.toString()})
  const {affectedRows} = await db.delete({
    form: 'shopping-cart',
    where: {
      id: value.ids,
      userid: user.id,
    },
  })
  if (!affectedRows) return ctx.error(431)
  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

// 计数
router.get('/count', async (ctx) => {
  const {user} = ctx.state
  if (!user) return ctx.error('401')
  const count = await db.count({
    form: 'shopping-cart',
    where: {
      userid: user.id,
    },
  })
  ctx.body = {
    code: 200,
    count,
  }
})

// 生成订单
router.post('/order', async (ctx) => {
  const {user} = ctx.state
  if (!user) return ctx.error('401')
  const {body} = ctx.request
  const {adopt, error, value} = shoppingCart.order.verifies(body)
  if (!adopt) return ctx.error(430, {error: error.toString()})
  const {affectedRows} = await db.update({
    form: 'shopping-cart',
    where: {
      userid: user.id,
      settle: 0,
      id: value.ids,
    },
    set: {
      settle: 1,
    },
  })
  if (!affectedRows) return ctx.error(431)
  let oid = (new Date()).toLocaleDateString().replace(/\//g, '')
  for (let i = 0; i < 6; i++) {
    oid += Math.floor(Math.random() * 10).toString()
  }
  const cs = await db.find({
    form: 'shopping-cart',
    field: ['commodityid'],
    virtual: ['price'],
    where: {
      userid: user.id,
      settle: 1,
      id: value.ids,
    },
  })
  const price = cs.reduce((a, b) => a + b.price, 0)
  console.log(cs)
  const data = db.insert({
    form: 'order',
    value: {
      id: oid,
      userid: user.id,
      scid: value.ids.join(','),
      price,
    },
  })
  ctx.body = {
    code: 200,
    affectedRows,
    id: oid,
    price,
  }
})

//待评价商品
router.get('/settled', async (ctx) => {
  const {user} = ctx.state
  if (!user) return ctx.error('401')
  const data = await db.find({
    form: 'shopping-cart',
    virtual: ['commodity'],
    where: {
      userid: user.id,
      settle: 2,
    },
  })
  ctx.body = {
    code: 200,
    data,
  }
})

module.exports = router.routes()
